package com.yoke.service.order;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoke.service.BaseService;
import com.yoke.util.JsonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by jiangzeyin on 2017/5/17.
 */
@Service
public class ProductService extends BaseService {

    @Resource
    private OrderService orderService;


    public String getProductTableName(String userName, String productName) throws IOException {
        JSONObject jsonObject = getUserProductJsonObj(userName, productName);
        if (jsonObject == null)
            return null;
        return jsonObject.getString("tableName");
    }


    public File getUserProductFile(String userName, String productName) {
        File customer = new File(getCustomerFile(), userName);
        return new File(customer, productName);
    }

    /**
     * 获取客户的产品信息  包括以删除
     *
     * @param name
     * @return
     * @throws IOException
     */
    public JSONArray getUserProductJson(String name) throws IOException {
        File user = getUserProductFile(name);// new File(getCustomerFile(), name + "/product.conf");
        if (!user.exists())
            return null;
        return (JSONArray) JsonUtil.readJson(user.getPath());
    }

    public JSONObject getUserProductJsonObj(String name, String productName) throws IOException {
        JSONArray jsonArray = getUserProductJson(name);
        if (jsonArray == null)
            return null;
        return getJSONObjectName(productName, jsonArray);
    }

    private File getProductInfoJsonFile(String userName, String productName) {
        File file = new File(getCustomerFile(), userName);
        file = new File(file, productName);
        file = new File(file, "product.json");
        if (!file.exists())
            return null;
        return file;
    }

    public JSONObject getProductInfoJsonObj(String userName, String productName) throws IOException {
        File file = getProductInfoJsonFile(userName, productName);
        if (file == null)
            return null;
        return (JSONObject) JsonUtil.readJson(file.getPath());
    }

    public File getUserProductFile(String userName) {
        File file = new File(getCustomerFile(), userName);
        return new File(file, "product.conf");
    }

    public void saveUserProduct(String userName, JSONArray jsonArray) throws IOException {
        File product = getUserProductFile(userName);
        JsonUtil.saveJson(product.getPath(), jsonArray);
    }

    /**
     * 获取所有产品  可以使用的
     *
     * @param userName
     * @return
     * @throws IOException
     */
    public JSONArray getUserAll(String userName) throws IOException {
        JSONArray jsonArray = getUserProductJson(userName);
        if (jsonArray == null)
            return null;
        JSONArray jsonArray1 = orderService.getOrderType();
        Iterator<Object> iterable = jsonArray.iterator();
        while (iterable.hasNext()) {
            JSONObject jsonObject = (JSONObject) iterable.next();// jsonArray.getJSONObject(i);
            String name = jsonObject.getString("name");
            if (name.endsWith("_del")) {
                iterable.remove();
                continue;
            }
            String orderType = jsonObject.getString("orderType");
            JSONObject typeObj = getJSONObjectName(orderType, jsonArray1);
            jsonObject.put("orderInfo", typeObj);
        }
        return sort(jsonArray);
    }

    /**
     * 排序
     *
     * @param jsonArray
     * @return
     */
    public static JSONArray sort(JSONArray jsonArray) {
        String time = "sort";
        jsonArray.sort((o1, o2) -> {
            JSONObject jsonObject1 = (JSONObject) o1;
            JSONObject jsonObject2 = (JSONObject) o2;
            int s1 = jsonObject1.getIntValue(time);
            int s2 = jsonObject2.getIntValue(time);
            return s1 - s2;
        });
        return jsonArray;
    }

    /**
     * @param typeName
     * @param jsonArray
     * @return
     */
    public JSONObject getJSONObjectName(String typeName, JSONArray jsonArray) {
        for (int i = jsonArray.size() - 1; i >= 0; i--) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String name = jsonObject.getString("name");
            if (name.equals(typeName))
                return jsonObject;
        }
        return null;
    }
}
